package com.monkeyk.os.service.util;

import com.monkeyk.os.domain.users.PasswordHandler;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Map;

/**
 * Created by Mountain on 2017/3/7.
 */
public class commonUtil {
    private static final  String secret="rogrand_community";
    public static String randomInt(int n) {
        double v = Math.random() * 9 + 1;//0~9范围
        double pow = Math.pow(10, n - 1);//扩展位数
        return String.valueOf((int) (v * pow));
    }
    /**
     * 签名
     *
     * @param params 需签名的参数
     */
    public static String sign(Map<String, String> params) {
        // 参数名排序
        String[] keyArray = params.keySet().toArray(new String[0]);
        Arrays.sort(keyArray);
        // 拼接参数
        StringBuilder temp = new StringBuilder();
        for (String key : keyArray) {
            temp.append(key);
            // 防止中文的参数值出现乱码，先转换为 UTF-8 编码
            Object value = params.get(key);
            if (null != value) {
                try {
                    String valueString = String.valueOf(value);
                    temp.append(URLEncoder.encode(valueString, "UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        }
        temp.append(secret);
        // MD5 加密
        return MD5Utils.getIstance().MD5Encode(temp.toString());
    }

}
